/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import persistence.SqlConnector;

/**
 * Singleton del Vocabulario
 * 
 * @author do.ob
 */
public class Vocabulario {

    private Map<String, Palabra> terminos;
    private List<Palabra> terminosList;
    private static Vocabulario vocabulario;

    private Vocabulario() {
        terminos = new HashMap<String, Palabra>();
        terminosList = new ArrayList<Palabra>();
        cargarVocabulario();
    }

    public static Vocabulario getInstance() {
        if (vocabulario == null) {
            return vocabulario = new Vocabulario();
        } else {
            return vocabulario;
        }
    }

    private void cargarVocabulario() {
        try {
            boolean debug = true;
            SqlConnector connector = new SqlConnector();
            connector.connect();

            ResultSet resultCantidadPalabras = connector.executeStatementQuery("SELECT COUNT(*) FROM vocabulario");
            resultCantidadPalabras.next();
            int cantidadPalabras = resultCantidadPalabras.getInt("Count(*)");
            System.out.println("Cantidad Palabras: " + cantidadPalabras);

            PreparedStatement pstmt = connector.setPreparedStatement("SELECT * FROM vocabulario LIMIT 10000 OFFSET ?");
                            int dup = 0;
            for (int i = 0; i <= cantidadPalabras; i = i + 10000) {
                debug = true;
                pstmt.setInt(1, i);
                ResultSet result = pstmt.executeQuery();

                Palabra palabra = null;

                while (result.next()) {                    
                    palabra = new Palabra(result.getInt("id"), result.getString("termino"), result.getInt("cantDoc"), result.getInt("maxTf"));
                    terminos.put(palabra.getTermino(), palabra);
                    
                }

            }
        } catch (SQLException ex) {
            Logger.getLogger(Vocabulario.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(Vocabulario.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public Palabra getPalabra(String termino) {
        return terminos.get(termino);
    }

    public Map<String, Palabra> getTerminos() {
        return terminos;
    }

    public void setTerminos(Map<String, Palabra> terminos) {
        this.terminos = terminos;
    }
}
